/**
 * @Copyright:Copyright (c) 2007 - 2023
 * 
 */
package com.tengtong.commons.json.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.tengtong.commons.base.KeyValue;
import com.tengtong.commons.collections.MapUtils;
import com.tengtong.commons.json.Json;

/**
 * 使用 Map 来实现 Json 接口
 *
 * @author lin_jinming
 * @since: v1.0
 */
public class MapJsonImpl implements Json{
    
    private Map<String, Object> datas;
    
    public MapJsonImpl() {
        this.datas = MapUtils.newLinkedHashMap();
    }
    
    public static MapJsonImpl of() {
        return new MapJsonImpl();
    }
    
    public static MapJsonImpl of(Map<String, Object> ds) {
        var result = of();
        result.datas = ds;
        return result;
    }
    
    public static MapJsonImpl of(String name, Object v) {
        var result = of();
        result.datas.put(name, v);
        return result;
    }

    /* (non-Javadoc)
     * @see com.tengtong.commons.json.Json#contains(java.lang.String)
     */
    @Override
    public boolean contains(String name) {        
        return this.datas.containsKey(name);
    }

    /* (non-Javadoc)
     * @see com.tengtong.commons.json.Json#getObject(java.lang.String)
     */
    @Override
    public Object getObject(String name) {
        
        return this.datas.get(name);
    }

    /* (non-Javadoc)
     * @see com.tengtong.commons.json.Json#getObject(java.lang.String, java.lang.Object)
     */
    @Override
    public Object getObject(String name, Object defaultValue) {
        if(this.contains(name)) {
            return this.datas.get(name);
        }
        return defaultValue;
    }

    /* (non-Javadoc)
     * @see com.tengtong.commons.json.Json#toMap()
     */
    @Override
    public Map<String, Object> toMap() {
        return this.datas;
    }

    /* (non-Javadoc)
     * @see com.tengtong.commons.json.Json#putData(java.lang.String, java.lang.Object)
     */
    @Override
    public Json putData(String name, Object value) {
        this.datas.put(name, value);
        return this;
    }

    @Override
    public String toString() {
        return this.datas.toString();
    }

    /* (non-Javadoc)
     * @see java.lang.Iterable#iterator()
     */
    @Override
    public Iterator<KeyValue> iterator() {
        Iterator<String> ks = datas.keySet().iterator();
        return new Iterator<KeyValue>() {
            
            @Override
            public boolean hasNext() {
                return ks.hasNext();
            }

            @Override
            public KeyValue next() {
                String key = ks.next();
                
                return new KeyValue(key, datas.get(key));
            }
            
        };

    }

    /* (non-Javadoc)
     * @see com.tengtong.commons.json.Json#getKeySet()
     */
    @Override
    public Set<String> getKeySet() {
        return this.datas.keySet();
    }

    /* (non-Javadoc)
     * @see com.tengtong.commons.json.Json#getValues()
     */
    @Override
    public Collection<Object> getValues() {
        return this.datas.values();
    }
}
